﻿/*
 * DG (DG Gaming Engine)  - API
 * Copyright (C) 2004  Bob Marks (marksie531@yahoo.com)
 * http://code.google.com/p/dng-gaming
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package com.dg.gaming.api.common.msg
{
	import nanoxml.XMLElement;
	import com.dg.gaming.api.common.TransmissionException;

	/**
	 * Communication object to send a key/value property from a client to server
	 * or vice versa.  Used in the DGController to simplify the sending and
	 * receiving of data.
	 *
	 * @author  Bob Marks
	 * @version Alpha 0.2.3
	 */
	public class MsgControllerProperty extends MsgTableMessage
	{

		/** Type is a String. */
		public static const TYPE_STRING:int=0;

		/** Type is an integer. */
		public static const TYPE_INT:int=1;

		/** Type is two integer e.g. co-ordinate for example. */
		public static const TYPE_INT_TWO:int=2;

		// Fields which hold the key and the value.
		private var key:String=null;
		private var value:String=null;

		private static const XML_ATT_KEY:String="key";
		private static const XML_ATT_VALUE:String="value";

		/**
		 * Constructor which takes a username, status, table number and a value.
		 *
		 * @param status       Type of message e.g. TYPE_STRING, TYPE_INT etc.
		 * @param key          Key
		 * @param value        Value
		 */
		public function MsgControllerProperty(status:int=-1, key:String=null, value:String=null, message:XMLElement=null)
		{

			initStatus(status);

			this.key=key;
			this.value=value;
			if (message != null)
				super(message);

			if (message != null)
			{
				if (message.name == Msg.CONTROLLER_PROPERTY)
				{
					throw new TransmissionException("Error parsing CommControllerProperty");
				}
				this.key=message.getStringAttribute(XML_ATT_KEY);
				this.value=message.getStringAttribute(XML_ATT_VALUE);
			}
		}


		/**
		 * Return the key.
		 *
		 * @return   Controller key.
		 */
		public function getKey():String
		{
			return key;
		}

		/**
		 * Return the value.
		 *
		 * @return   Controller property.
		 */
		public function getValue():String
		{
			return value;
		}

		/**
		 * Flatten communciation object.
		 *
		 * @see com.dg.gaming.api.common.msg.ITransmittable#flatten()
		 */
		public override function flatten():XMLElement
		{
			var message:XMLElement=flattenWithName(Msg.CONTROLLER_PROPERTY);
			message.setAttribute(XML_ATT_KEY, key);
			message.setAttribute(XML_ATT_VALUE, value);

			return message;
		}
	}
}